﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;

namespace DuJianliang.ApplicationPlatform.UserPermission
{
    public partial class PasswordEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string oldPassword = txt1.Text.Trim().ToLower();
            if (oldPassword.Equals(CurrentInfo.Instance.CurrentUser.Password.ToLower()))
            {
                if (!txt2.Text.Trim().Equals(txt3.Text.Trim()))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('2次输入新密码不一致。');</script>");
                }
                else
                {
                    ModifyPassword(CurrentInfo.Instance.CurrentUser.ID, txt2.Text.Trim());
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('密码修改成功。');</script>");
                }
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('旧密码错误。修改失败。');</script>"); 
            }
        }

        private void ModifyPassword(string userid, string password)
        {
            string sql = @"UPDATE dbo.t5715_User SET [Password] = @Password WHERE UserID = @UserID";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@UserID", DbType.String, userid);
                database.AddInParameter(dbCommand, "@Password", DbType.String, password);
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}